package com.mls.fivedistrict.controller.portal;


import com.baomidou.mybatisplus.mapper.Condition;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.mls.fivedistrict.common.ServiceResponse;
import com.mls.fivedistrict.controller.BaseController;
import com.mls.fivedistrict.exception.CustomerException;
import com.mls.fivedistrict.pojo.NewsComment;
import com.mls.fivedistrict.pojo.NewsFlashLikeRecord;
import com.mls.fivedistrict.pojo.UserLikeComment;
import com.mls.fivedistrict.server.NewsCommentService;
import com.mls.fivedistrict.server.UserLikeCommentService;
import com.mls.fivedistrict.util.validator.BeanValidator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import java.util.Optional;

/**
 * <p>
 * 用户点赞评论 前端控制器
 * </p>
 *
 * @author Five Team
 * @since 2018-06-29
 */
@RestController("protalUserLikeCommentController")
@RequestMapping("/api/app")
public class UserLikeCommentController extends BaseController {
    @Autowired
    UserLikeCommentService userLikeCommentService;
    @Autowired
    NewsCommentService newsCommentService;

    /**
     * 用户点赞评论接口
     *
     * @return
     */
    @Transactional
    @PostMapping("/v1/userLikeComment")
    public ServiceResponse addUserLikeComment(@RequestBody UserLikeComment userLikeComment) {
        userLikeComment.setUserId(getUser().getId());
        BeanValidator.check(userLikeComment);
        boolean flag = userLikeCommentService.insert(userLikeComment);
        Long commentId = userLikeComment.getCommentId();
        if(flag){
            flag=newsCommentService.addLikeTimes(commentId);
        }
        if (flag) {
            return ServiceResponse.createSuccessResponse("点赞评论成功");
        } else {
            return ServiceResponse.createErrorResponse("发生未知异常，点赞失败");
        }
    }

    /**
     * 取消评论点赞接口
     *
     * @param userLikeCommentId
     * @return
     */
    @Transactional
    @DeleteMapping("/v1/userLikeComment/{userLikeCommentId}")
    public ServiceResponse deleteNewsFlashLikeRecord(@PathVariable Long userLikeCommentId) {
        Optional.ofNullable(userLikeCommentId).orElseThrow(() -> new CustomerException("评论的点赞id不能为空"));
        Wrapper wrapper = Condition.create();
        wrapper.eq("user_id", getUser().getId());
        wrapper.eq("id", userLikeCommentId);
        UserLikeComment userLikeComment = userLikeCommentService.selectOne(wrapper);
        Optional.ofNullable(userLikeComment).orElseThrow(() -> new CustomerException("查无此条点赞记录"));
        Long commentId = userLikeComment.getCommentId();
        boolean b = userLikeCommentService.deleteById(userLikeComment);
        if (b) {
            b = newsCommentService.cutLikeTimes(commentId);
        }
        if (b) {
            return ServiceResponse.createSuccessResponse("取消点赞成功!");
        }
        return ServiceResponse.createErrorResponse("发生未知异常，取消点赞失败");
    }
}

